package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.LmysdjlbBsDetailBo;
import com.ruoyi.report_ljlm.domain.vo.LmysdjlbBsDetailVo;
import com.ruoyi.report_ljlm.domain.LmysdjlbBsDetail;
import com.ruoyi.report_ljlm.mapper.LmysdjlbBsDetailMapper;
import com.ruoyi.report_ljlm.service.ILmysdjlbBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面压实度试验检测记录(钻芯法)详情Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class LmysdjlbBsDetailServiceImpl implements ILmysdjlbBsDetailService {

    private final LmysdjlbBsDetailMapper baseMapper;

    /**
     * 查询路面压实度试验检测记录(钻芯法)详情
     */
    @Override
    public LmysdjlbBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询路面压实度试验检测记录(钻芯法)详情列表
     */
    @Override
    public TableDataInfo<LmysdjlbBsDetailVo> queryPageList(LmysdjlbBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LmysdjlbBsDetail> lqw = buildQueryWrapper(bo);
        Page<LmysdjlbBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面压实度试验检测记录(钻芯法)详情列表
     */
    @Override
    public List<LmysdjlbBsDetailVo> queryList(LmysdjlbBsDetailBo bo) {
        LambdaQueryWrapper<LmysdjlbBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LmysdjlbBsDetail> buildQueryWrapper(LmysdjlbBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LmysdjlbBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, LmysdjlbBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), LmysdjlbBsDetail::getShijbh, bo.getShijbh());
        lqw.eq(StringUtils.isNotBlank(bo.getQvywz()), LmysdjlbBsDetail::getQvywz, bo.getQvywz());
        lqw.eq(bo.getShijhd() != null, LmysdjlbBsDetail::getShijhd, bo.getShijhd());
        lqw.eq(bo.getGanzsjkqzzl() != null, LmysdjlbBsDetail::getGanzsjkqzzl, bo.getGanzsjkqzzl());
        lqw.eq(bo.getShijszzl() != null, LmysdjlbBsDetail::getShijszzl, bo.getShijszzl());
        lqw.eq(bo.getShijbgzl() != null, LmysdjlbBsDetail::getShijbgzl, bo.getShijbgzl());
        lqw.eq(bo.getShijmtjxdmd() != null, LmysdjlbBsDetail::getShijmtjxdmd, bo.getShijmtjxdmd());
        lqw.eq(bo.getShijmtjmd() != null, LmysdjlbBsDetail::getShijmtjmd, bo.getShijmtjmd());
        lqw.eq(bo.getKongxl() != null, LmysdjlbBsDetail::getKongxl, bo.getKongxl());
        lqw.eq(bo.getBiaozmdysd() != null, LmysdjlbBsDetail::getBiaozmdysd, bo.getBiaozmdysd());
        lqw.eq(bo.getLilmdysd() != null, LmysdjlbBsDetail::getLilmdysd, bo.getLilmdysd());
        return lqw;
    }

    /**
     * 新增路面压实度试验检测记录(钻芯法)详情
     */
    @Override
    public Boolean insertByBo(LmysdjlbBsDetailBo bo) {
        LmysdjlbBsDetail add = BeanUtil.toBean(bo, LmysdjlbBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改路面压实度试验检测记录(钻芯法)详情
     */
    @Override
    public Boolean updateByBo(LmysdjlbBsDetailBo bo) {
        LmysdjlbBsDetail update = BeanUtil.toBean(bo, LmysdjlbBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LmysdjlbBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面压实度试验检测记录(钻芯法)详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
